package com.mifengkong.statistic.alipay;

import android.app.Activity;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.mifengkong.frtools.util.FRLog;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.statistic.alipay.util.PayResult;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by jack on 2018/1/15
 * 支付管理器
 */

public class PayManager {

    private Activity activity;
    private OnAliPaySuccess onAliPaySuccess;

    private boolean isNeedH5;  //是否支持H5（在没有安装支付宝app的情况下）

    public PayManager(Activity activity) {
        this.activity = activity;
    }

    /**
     * 支付宝支付业务
     *
     * @param orderInfo 订单信息，所有的都由后端给，包括加签名
     */
    public void goToAliPay(final String orderInfo) {
        if (FRString.isEmpty(orderInfo)) {
            throw new IllegalArgumentException("orderInfo should not be null");
        }
        Observable.create(new ObservableOnSubscribe<Map<String, String>>() {
            @Override
            public void subscribe(ObservableEmitter<Map<String, String>> e) throws Exception {
                PayTask alipay = new PayTask(activity);
                Map<String, String> result = alipay.payV2(orderInfo, isNeedH5);
                e.onNext(result);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Map<String, String>>() {
                    @Override
                    public void accept(Map<String, String> stringStringMap) throws Exception {
                        PayResult payResult = new PayResult(stringStringMap);
                        /**
                         对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                         */
                        String resultStatus = payResult.getResultStatus();
                        // 判断resultStatus 为9000则代表支付成功
                        if (TextUtils.equals(resultStatus, "9000")) {
                            // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                            if (null != onAliPaySuccess) {
                                onAliPaySuccess.paySuccess();
                            }
                        } else {
                            FRLog.error("resultStatus--->" + resultStatus);
                            if (null != onAliPaySuccess) {
                                onAliPaySuccess.payFailure();
                            }
                        }
                    }
                });
    }

    public void setNeedH5(boolean needH5) {
        isNeedH5 = needH5;
    }

    public void setOnAliPaySuccess(OnAliPaySuccess onAliPaySuccess) {
        this.onAliPaySuccess = onAliPaySuccess;
    }

    /**
     * 支付回调（成功，失败）
     */
    public interface OnAliPaySuccess {
        void paySuccess();

        void payFailure();
    }
}
